﻿using System;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using C1.Silverlight.DataGrid.Summaries;

namespace DYSales.Views.Stock
{
    public partial class mgInventory_page : Page
    {
        public mgInventory_page()
        {
            InitializeComponent();
            dg_ivs.AutoGeneratingColumn += new EventHandler<C1.Silverlight.DataGrid.DataGridAutoGeneratingColumnEventArgs>(dg_ivs_AutoGeneratingColumn);
            Loaded += new RoutedEventHandler(mgInventory_page_Loaded);
            cbb_盘点批次.SelectionChanged += new SelectionChangedEventHandler(cbb_盘点批次_SelectionChanged);
            bt_显示所有.Click += new RoutedEventHandler(bt_显示所有_Click);
            bt_删除.Click += new RoutedEventHandler(bt_删除_Click);
        }

        void bt_删除_Click(object sender, RoutedEventArgs e)
        {
            if (cbb_盘点批次.SelectedIndex != -1)
            {
                var its = dm.getDS().Inventories.Where(i => i.p_盘点日期.Equals((DateTime)cbb_盘点批次.SelectedValue));
                if (its.Count() > 0)
                {
                    foreach (var item in its.ToList())
                    {
                        dm.getDS().Inventories.Remove(item);
                    }
                    dm.getDS().SubmitChanges(lo => {
                        refresh();
                    }, null);
                }
            }
        }

        void bt_显示所有_Click(object sender, RoutedEventArgs e)
        {
            refresh();
        }

        void cbb_盘点批次_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (cbb_盘点批次.SelectedValue != null)
            {
                dg_ivs.DataContext = null;
                dg_ivs.DataContext = dm.getDS().Inventories.Where(i => i.p_盘点日期.Equals((DateTime)cbb_盘点批次.SelectedValue));
            }
        }

        void dg_ivs_AutoGeneratingColumn(object sender, C1.Silverlight.DataGrid.DataGridAutoGeneratingColumnEventArgs e)
        {
            if (e.Property.PropertyType.Equals(typeof(decimal)) || e.Property.PropertyType.Equals(typeof(double)))
            {
                e.Column.Format = "0.##";
            }
            if (e.Property.PropertyType.Equals(typeof(DateTime)))
            {
                e.Column.Format = "yyyy/MM/dd HH:mm:ss";
            }
            #region 减少列
            string headername = e.Column.Header.ToString();
            switch (headername)
            {
                case "ID":
                case "库存下限":
                case "进货价":
                    e.Cancel = true;
                    break;

                case "库存数量":
                    DataGridAggregate.SetAggregateFunctions(e.Column, new DataGridAggregatesCollection { new DataGridAggregateSum() });
                    break;
                case "盘点数量":
                    DataGridAggregate.SetAggregateFunctions(e.Column, new DataGridAggregatesCollection { new DataGridAggregateSum() });
                    break;
                case "盘点结果":
                    DataGridAggregate.SetAggregateFunctions(e.Column, new DataGridAggregatesCollection { new DataGridAggregateSum() });
                    break;
            }
            #endregion
        }

        void mgInventory_page_Loaded(object sender, RoutedEventArgs e)
        {
            refresh();
        }

        void refresh()
        {
            cbb_盘点批次.DataContext = null;
            dm.getDS().Load(dm.getDS().GetInventorysQuery(), lo => {
                dg_ivs.DataContext = lo.Entities;
                var skin = lo.Entities.GroupBy(i => i.p_盘点日期).Select(i => i.First());
                cbb_盘点批次.DataContext = skin.Select(i=>i.p_盘点日期).ToList();
            }, null);
        }

        // Executes when the user navigates to this page.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }

    }
}
